package com.thejaswi.commons;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import java.util.Properties;
import java.util.Iterator;
import java.io.File;
import java.util.Map;
import java.io.FileInputStream;

/**
 *
 * @author BhatG
 */
public class Configurator {
    private Properties config=new Properties();
    /**
     * @param args the command line arguments
     */
   public Configurator() {
      this.addOSenv();
      this.addSystemProperties();
   }

   public Configurator(String[] args) {
      this.addOSenv();
      this.addSystemProperties();
      // process command line variable to get name of configFile, if any.
      this.addArguments(args);
   }

   // static class to bundle all variable values into a single set
   public Properties getProperties() {
      return this.config;
   }

   public String getProperty(String key) {
      return this.config.getProperty(key);
   }

   public String getPropertyIgnoreCase(String key) {
      String retValue="";
      Iterator<Object> iter=config.keySet().iterator();
      boolean exitFlag=false;
      // loop while iterator has more values and exitFlag is still false
      while (iter.hasNext() && ! exitFlag ) {
         retValue=config.getProperty( (String) iter.next());
         if ( retValue.equalsIgnoreCase(key) ) {
            exitFlag=true;
         }
      }
      retValue=(exitFlag ? retValue : "" );
      return retValue;
   }

   public void load(String[] args) {
      this.addArguments(args);
   }

   public void loadFile(String configFilePath) {
      // look for a config file and use that to add values to config
      this.addConfigFileProperties(configFilePath);
   }

   private void addOSenv() {
         // First, get all OS environment variables
         Map<String, String> env=System.getenv();
         for (String envName: env.keySet() ) {
            config.setProperty(envName, env.get(envName));
         } // for env.keySet loop
   }

   private void addSystemProperties() {
      // copy all java system variable values to the config property
      for (Iterator<Object> iter=System.getProperties().keySet().iterator(); iter.hasNext();) {
        String name=(String) iter.next();
        String value=System.getProperty(name);
        config.setProperty(name, value);
      } // for loop for jdk/jre system variables
   }

   private void addConfigFileProperties(String configFilePath) {
      if ( configFilePath.length()>0 ) {
         File cfgFile = new File(configFilePath);
         if ( cfgFile.exists() && cfgFile.canRead() && cfgFile.length()>0 ) {
            // load configuration information
            try {
               // create and load default properties
               FileInputStream ins = new FileInputStream(configFilePath);
               config.load(ins);
               ins.close();
            }
            catch (Exception e) {
               e.printStackTrace();
            } // catch exception
         } // if cfgFile.exists
      } // if configFilePath.length > 0
   }

   // add values from parameter string array
   private void addArguments(String[] args) {
      // add command line arguments
      for (String arg: args) {
        String argSplit[]=arg.split("=");
        if (argSplit.length>0) {
           // get the name and value, remove leading/trailing spaces
           String name=argSplit[0].trim();
           String value=argSplit[1].trim();
           // cleanup the name portion: remove initial -, if any
           name=name.replace("-","");
           this.config.setProperty(name, value);
        } // if argSplit.length > 0
      } // for args loop
   } // end of addCommandLineArguments

}
